app.controller("customerAdd",function($scope,$http, $stateParams,get,post,$rootScope,Md5,$state){
	
	//取消
	$scope.rement=function(){
		$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
	}
	
	$scope.selects=function(){
		//重要程度
		get.list("dictionary/importanceDic").then(function(e){
				$scope.importanceDic = e
					//专业
			})
		//销售负责人
		get.list("dictionary/staffSale").then(function(e){
				$scope.staffSale = e
			})
		//所属公司
		get.list("dictionary/saleCompany").then(function(e){
				$scope.saleCompany = e
			})
		
		//重要程度
			$scope.cImportance={
						value:{
							diName:"请选择",
							diId:null
						}
					}
			//所属公司
			$scope.staffId={
						value:{
							diName:"请选择",
							diId:null
						}
					}
			//销售负责人0
			$scope.companyId={
						value:{
							diName:"请选择",
							diId:null
						}
					}
		
	}
	$scope.selects()
	if($stateParams.id!=''){
		$scope.isshows=true
		get.list('customer/findOne',{cId:$stateParams.id}).then(function(e){
		$scope.add={
			cId:e.cId,
		cContact:e.cContact,
		cCPoltow:e.cCPoltow,
		cCPoltree:e.cCPoltree,
		cTelephone:e.cTelephone,
		cOfficePhone:e.cOfficePhone,
		cPost:e.cPost, 
		cImportance:e.cImportance,
		staffId:e.staffId,
		companyId:e.companyId,
		cRemark:e.cRemark
		
	}
			
			//重要程度
	angular.forEach($scope.importanceDic,function(o){
		if(e.cImportance==o.diId){
			$scope.cImportance={
				value:o
			}
		}
	})
	//所属销售
	angular.forEach($scope.staffSale,function(o){
		if(e.staffId==o.diId){
			$scope.staffId={
					value:o
			}
	
		}
	})
	//所属公司
	angular.forEach($scope.saleCompany,function(o){
		if(e.companyId==o.diId){
			$scope.companyId={
					value:o
			}
		}
	})
	
		})
	}
	
	
	$scope.add={
		cContact:'',
		cCPoltow:'',
		cCPoltree:'',
		cTelephone:'',
		cOfficePhone:'',
		cPost:'', 
		cImportance:'',
		staffId:'',
		companyId:'',
		cRemark:''
		
	}
	$scope.sub=function(val){
	
		if($scope.add.cImportance!=null){
			$scope.add.cImportance=$scope.add.cImportance.diId||$scope.add.cImportance
		}
		if($scope.add.companyId!=null){
			$scope.add.companyId=$scope.add.companyId.diId||$scope.add.companyId
		}
		if($scope.add.staffId!=null){
			$scope.add.staffId=$scope.add.staffId.diId||$scope.add.staffId
		}
		$scope.add.cImportance=$scope.add.cImportance||''
		$scope.add.staffId=$scope.add.staffId||''
		$scope.add.companyId=$scope.add.companyId||''
		if($stateParams.id!=''){
				post.list("customer/update",$scope.add).then(function(o){
						$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
				})
			}else{
				post.list("customer/add",$scope.add).then(function(o){
						$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
				})
				
			}
			
			
		
		
	}
	
	
})
